package com.liveneo.adapter.outinterface.thread;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.liveneo.adapter.base.entity.PrpmCaseInfoCyp;
import com.liveneo.adapter.base.entity.PrpmUploadLossItemImg;
import com.liveneo.adapter.base.entity.PrpmUploadTask;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.outinterface.util.DBTableUtil;
import com.liveneo.adapter.outinterface.util.ResultInfo;
import com.liveneo.adapter.outinterface.util.SpringConfigTool;
import com.liveneo.adapter.outinterface.util.StringUtil;
import com.liveneo.adapter.outinterface.web.platform.ClaimInterface;
import com.liveneo.adapter.outinterface.web.platform.request.CliamUploadDto;
import com.liveneo.system.base.utils.OssManage;

/**
  * @Description: 视频上传核心线程
  * @author liveneo-shuqw
  * @date 2016年10月20日 下午10:43:28
  * @Copyright: Copyright (c) 2016 
  * @Company: 北京朗泰
  * @version 3.0
  */
public class UploadLossItemIdCypImageThread implements Runnable{

	private String caseNo; //案件号
	private String lossItemCypId; //用户编号
	private int pageNo; //第几页
	private DBTableUtil dbTableUtil = (DBTableUtil) SpringConfigTool.getBean("DBTableUtil");
	
	public UploadLossItemIdCypImageThread() {
		super();
	}

	public UploadLossItemIdCypImageThread(String caseNo,String lossItemCypId, int pageNo) {
		super();
		this.caseNo = caseNo;
		this.lossItemCypId = lossItemCypId;
		this.pageNo = pageNo;
	}


	@Override
	public void run() {
		LogUtils.info("影像资料视频上传----->P04LossItemCyp-LossItemCypId线程启动start-------------------->");
		InputStream in = null;
		try {
			OssManage ossManage = new OssManage();
			// 获取图片列表
			List<PrpmUploadLossItemImg> cypImageList = dbTableUtil.selectLossItemCypCountByIdList(lossItemCypId, pageNo);
			List<String> imgNameList = new ArrayList<String>();
			List<CliamUploadDto> uploadDtos = new ArrayList<CliamUploadDto>();
			for (PrpmUploadLossItemImg lossItemImg : cypImageList) {
				// 去重
				if (!imgNameList.contains(lossItemImg.getImgName())) {
					try {
						// 获取图片流
						in = new BufferedInputStream(ossManage.getObject(lossItemImg.getOssKey()));
					} catch (Exception e) {
						LogUtils.error("oss获取流异常：",e);
						continue;
					}
					// 上传图片信息
					CliamUploadDto cliamUploadDto = new CliamUploadDto();
					cliamUploadDto.setFileStream(in);
					// 图片名称
					String imgName = lossItemImg.getImgName();
					cliamUploadDto.setImgName(imgName);
					// 图片类型
					cliamUploadDto.setImgType(StringUtil.getFileName(imgName));
					uploadDtos.add(cliamUploadDto);
					// 添加重名
					imgNameList.add(imgName);
				}
			}
			if (uploadDtos.size() > 0) {
				PrpmCaseInfoCyp caseInfo = dbTableUtil.selectCaseInfoCypByNo(caseNo);
				// 上传
				if(caseInfo != null){
					uploadLossItemCypComm(caseInfo.getCaseInfoCypId(),cypImageList, uploadDtos);
				}
			}

		} catch (Exception e) {
			LogUtils.error("接口影视图片上传[P04----->uploadImg]" + caseNo + "上传视频服务器异常：\n", e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
				}
			}
			LogUtils.info("影像资料视频上传----->P04LossItemCyp-LossItemCypId线程启动end-------------------->");
		}
	}
	
	/**
	  * @Title: uploadImgVideoComm
	  * @Description: 图片视频上传
	  * @param @param caseUuid
	  * @param @param imgList
	  * @param @param uploadDtos
	  * @param @throws Exception 设定文件
	  * @return void 返回类型
	  * @throws
	  */
	private void uploadLossItemCypComm(String caseid,List<PrpmUploadLossItemImg> cypImageList, List<CliamUploadDto> uploadDtos)
			throws Exception {
		// 上传到理赔核心
		ResultInfo resultInfo = ClaimInterface.upoadImage(caseNo, uploadDtos);
		String isSuccessful = "0";
		if ("000".equals(resultInfo.getResultCode())) {
			// 上传成功
			updateStayUpload(cypImageList, "2");
			isSuccessful = "2";
		} else {
			// 上传失败
			updateStayUpload(cypImageList, "3");
		}
		// 添加定义task
		PrpmUploadTask uploadTask = dbTableUtil.selectUploadTaskByCaseId(caseid, "0");
		if (uploadTask != null) {
			// 更新
			dbTableUtil.updateUploadTaskById(isSuccessful, uploadTask.getUpTimes()+1, uploadTask.getUploadTaskId());
		} else {
			// 保存
			dbTableUtil.saveUploadTask(caseid, caseNo,cypImageList.get(0).getLossItemCypId(),"1");
		}
	}

	/**
	  * @Title: updateStayUpload
	  * @Description: 更新状态
	  * @param @param imgList
	  * @param @param stayUpload
	  * @param @throws Exception 设定文件
	  * @return void 返回类型
	  * @throws
	 */
	private void updateStayUpload(List<PrpmUploadLossItemImg> cypImageList,String stayUpload) throws Exception{
		for (PrpmUploadLossItemImg lossItemImg : cypImageList) {
			dbTableUtil.updateImageLossItemCypStayUploadById(stayUpload, lossItemImg.getUploadLossItemImgId());
		}
	}


	public String getCaseNo() {
		return caseNo;
	}


	public void setCaseNo(String caseNo) {
		this.caseNo = caseNo;
	}

	public String getLossItemCypId() {
		return lossItemCypId;
	}

	public void setLossItemCypId(String lossItemCypId) {
		this.lossItemCypId = lossItemCypId;
	}

	public int getPageNo() {
		return pageNo;
	}


	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
}
